# \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
# (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
# {ip:count, ip:count, ip:count}
import re

# re_obj = re.compile('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
#
def count_log(file, re_str):
    re_obj = re.compile(re_str)
    with open(file, mode='r') as fr:
        mydict = {}
        for line in fr.readlines():
            # print(line)
            ip = re_obj.search(line).group()
            # print(ip.group())
            if ip not in mydict:
                mydict[ip] = 1
            else:
                mydict[ip] += 1
    return mydict

if __name__ == '__main__':
    ip_str = '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
    print(count_log('apache_log.txt', ip_str))



